﻿using Mono.Cecil;

namespace il2lua
{
    /// <summary>
    /// il module to lua module
    /// </summary>
    public class LuaModule
    {
        public string Name { get; }
        public ModuleContext Context { get; }
        public IList<LuaType> Types { get; }
        public LuaModule(string path)
        {
            var assembly = AssemblyDefinition.ReadAssembly(path);
            Name = Path.GetFileNameWithoutExtension(path);
            var module = assembly.MainModule;
            module.ReadSymbols();
            Context = new ModuleContext();
            Types = new List<LuaType>();
            foreach (var type in module.Types)
            {
                if (Exclude(type))
                    continue;
                Types.Add(new LuaType(this, type));
            }
        }
        private bool Exclude(TypeDefinition type)
        {
            if (type.FullName == "<Module>")
                return true;
            else if (type.FullName == "Microsoft.CodeAnalysis.EmbeddedAttribute")
                return true;
            else if (type.FullName == "System.Runtime.CompilerServices.NullableAttribute")
                return true;
            else if (type.FullName == "System.Runtime.CompilerServices.NullableContextAttribute")
                return true;
            return false;
        }
    }
}
